import http from '@/utils/http'

const BASE_URL = '/shop/coupon'

export interface CouponTemplateQuery extends SortQuery {
  name?: string
  status?: number
}

export interface CouponTemplateName {
  id: string
  name: string
  image: string
  discountType: string
  discountAmount: number
  maxDiscountAmount: number
  thresholdAmount: number

  classIdLimit?: []
  classCategoryLimit?: []
  classSubjectLimit?: []
  classGradeLimit?: []
  classTermLimit?: []
  classYearLimit?: []

  stackingUseEnabled: boolean
  receiveLimitCount: number
  validType: string
  startValidTime: string
  endValidTime: string
  validDays: number

  status: number
  description: string
}

export interface CouponTemplateResp {
  distributedCount: number
  receivedCount: number
  usedCount: number
}

export interface CouponQuery {
  name?: string
  parentPhone?: string
  couponStatus?: string | number
  orderId?: string
}

export interface ParentCouponQuery {
  parentId: string
  couponStatus?: number
  orderId?: string
}

export interface Coupon {
  id: string
  parentId: string
  couponTemplateId: string
  receiveType: string
  receiveTime: string
  couponStatus: string
  useTime: string
  orderId: string
}

export interface CouponResp extends Coupon {
  couponTemplateName: string
  parentName: string
  parentPhone: string
}

export interface CouponDetail extends Coupon {
  template: CouponTemplateName
  startValidTime: string
  endValidTime: string

}

export interface CouponDistributeReq {
  couponTemplateId: string
  parentId: string
}

export interface CouponImportReq {
  importKey: string
}

export interface CouponImportParseResp {
  importKey: string
  totalRows: number
  validRows: number
}

export interface CouponImportResp {
  totalRows: number
  insertRows: number
  updateRows: number
}

export interface CouponTemplateReq {
  name: string
  image?: string
  discountType: string
  discountAmount: number
  maxDiscountAmount: number
  thresholdAmount: number

  sigleClassIdLimit?: string
  classCategoryLimit?: []
  classSubjectLimit?: []
  classGradeLimit?: []
  classTermLimit?: []
  classYearLimit?: []

  stackingUseEnabled: boolean
  receiveLimitCount: number
  validType: string
  startValidTime?: string
  endValidTime?: string
  validDays?: number

  status: number
  description?: string
}

export const createCouponTemplate = (data: CouponTemplateReq) => {
  return http.post(`${BASE_URL}/template/create`, data)
}

export const getCouponTemplatePage = (query: CouponTemplateQuery & PageQuery) => {
  return http.get<Page<CouponTemplateResp[]>>(`${BASE_URL}/template/page`, query)
}

export const deleteCouponTemplate = (id: string) => {
  return http.post(`${BASE_URL}/template/delete/${id}`)
}

export const updateCouponTemplate = (data: CouponTemplateReq, id: string) => {
  return http.post(`${BASE_URL}/template/update/${id}`, data)
}

export const getCouponTemplateDetail = (id: string) => {
  return http.get<CouponTemplateResp>(`${BASE_URL}/template/detail/${id}`)
}

export const getCouponPage = (query: CouponQuery | PageQuery) => {
  return http.get(`${BASE_URL}/page`, query)
}

export const getParentCouponList = (query: ParentCouponQuery) => {
  return http.get<CouponDetail[]>(`${BASE_URL}/list-by-parent`, query)
}

export const removeCoupon = (id: string) => {
  return http.post(`${BASE_URL}/delete/${id}`)
}

export const distributeCoupon = (data: CouponDistributeReq) => {
  return http.post(`${BASE_URL}/distribute`, data)
}

export const cancelCoupon = (id: string) => {
  return http.post(`${BASE_URL}/cancel/${id}`)
}

// 下载模板
export function downloadImportCouponTemplate() {
  return http.download(`${BASE_URL}/downloadImportCouponTemplate`)
}

/** 文件上传 */
export function upload(query: CouponImportReq) {
  return http.post(`${BASE_URL}/import`, query)
}

// 解析导入数据
export function parseImportCoupon(formData: any) {
  return http.post(`${BASE_URL}/parseImportCoupon`, formData)
}
